package com.lswms.modules.lswms.service.impl.receipt;

import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.lswms.common.constant.Constant;
import com.lswms.common.page.PageData;
import com.lswms.common.service.impl.CrudServiceImpl;
import com.lswms.modules.lswms.MEGConstant;
import com.lswms.modules.lswms.Constant.InventoryConstant;
import com.lswms.modules.lswms.Constant.LocationConstant;
import com.lswms.modules.lswms.Constant.ReceiptConstant;
import com.lswms.modules.lswms.Constant.TaskConstant;
import com.lswms.modules.lswms.Constant.TradeConstant;
import com.lswms.modules.lswms.dao.base.SkuDao;
import com.lswms.modules.lswms.dao.receipt.ReceiptDao;
import com.lswms.modules.lswms.dao.receipt.ReceiptdetailDao;
import com.lswms.modules.lswms.dao.receipt.ReceiptdetaillistDao;
import com.lswms.modules.lswms.dao.stock.InventoryDao;
import com.lswms.modules.lswms.dao.stock.LotDao;
import com.lswms.modules.lswms.dao.stock.TradeDao;
import com.lswms.modules.lswms.dao.task.TaskDao;
import com.lswms.modules.lswms.dao.uniquecode.UniquecodeDao;
import com.lswms.modules.lswms.dao.uniquecode.UniquecoderecordDao;
import com.lswms.modules.lswms.dto.receipt.ReceiptDTO;
import com.lswms.modules.lswms.entity.base.SkuEntity;
import com.lswms.modules.lswms.entity.receipt.ReceiptEntity;
import com.lswms.modules.lswms.entity.receipt.ReceiptdetailEntity;
import com.lswms.modules.lswms.entity.receipt.ReceiptdetaillistEntity;
import com.lswms.modules.lswms.entity.stock.InventoryEntity;
import com.lswms.modules.lswms.entity.stock.LotEntity;
import com.lswms.modules.lswms.entity.stock.TradeEntity;
import com.lswms.modules.lswms.entity.task.TaskEntity;
import com.lswms.modules.lswms.entity.uniquecode.UniquecodeEntity;
import com.lswms.modules.lswms.entity.uniquecode.UniquecoderecordEntity;
import com.lswms.modules.lswms.service.base.SequenceService;
import com.lswms.modules.lswms.service.receipt.ReceiptService;
import com.lswms.modules.lswms.service.receipt.ReceiptdetaillistService;

/**
 * 收货
 *
 * @author DX sunlightcs@gmail.com
 * @since 1.0.0 2021-06-13
 */
@Service
public class ReceiptServiceImpl extends CrudServiceImpl<ReceiptDao, ReceiptEntity, ReceiptDTO>
		implements ReceiptService {

	@Autowired
	ReceiptdetailDao receiptdetailDao;

	@Autowired
	ReceiptdetaillistDao receiptdetaillistDao;
	
	@Autowired
	ReceiptdetaillistService receiptdetaillistService ;

	@Autowired
	InventoryDao inventoryDao;

	@Autowired
	SequenceService sequenceService;

	@Autowired
	TradeDao tradeDao;

	@Autowired
	TaskDao taskDao;

	@Autowired
	SkuDao skuDao;

	@Autowired
	LotDao lotDao;

	@Autowired
	private UniquecodeDao uniquecodeDao;

	@Autowired
	private UniquecoderecordDao uniquecoderecordDao;

	@Override
	public QueryWrapper<ReceiptEntity> getWrapper(Map<String, Object> params) {
		String id = (String) params.get("id");

		QueryWrapper<ReceiptEntity> wrapper = new QueryWrapper<>();
		wrapper.eq(StringUtils.isNotBlank(id), "id", id);

		return wrapper;
	}

	@Override
	public PageData<ReceiptEntity> pageRequest(HttpServletRequest request) throws ParseException {

		Map<String, Object> param = new HashMap<String, Object>();
		param.put(Constant.PAGE, request.getParameter(Constant.PAGE));
		param.put(Constant.LIMIT, request.getParameter(Constant.LIMIT));
		param.put(Constant.ORDER_FIELD, request.getParameter(Constant.ORDER_FIELD));
		param.put(Constant.ORDER, request.getParameter(Constant.ORDER));

		String id = request.getParameter("id");
		String receiptkey = request.getParameter("receiptkey");
		String type = request.getParameter("type");
		String status = request.getParameter("status");
		String sourcekey = request.getParameter("sourcekey");
		String supplierkey = request.getParameter("companyid");
		String suppliername = request.getParameter("companyname");
		String[] addtime = request.getParameterValues("addtime");

		QueryWrapper<ReceiptEntity> wrapper = new QueryWrapper<>();
		wrapper.eq(StringUtils.isNotBlank(id), "id", id);
		wrapper.eq(StringUtils.isNotBlank(receiptkey), "receiptkey", receiptkey);
		wrapper.eq(StringUtils.isNotBlank(type), "type", type);
		wrapper.eq(StringUtils.isNotBlank(status), "status", status);
		wrapper.eq(StringUtils.isNotBlank(sourcekey), "sourcekey", sourcekey);
		wrapper.eq(StringUtils.isNotBlank(supplierkey), "companyid", supplierkey);
		wrapper.eq(StringUtils.isNotBlank(suppliername), "companyname", suppliername);
		if (null != addtime && addtime.length == 2) {
			if (StringUtils.isNotBlank(addtime[0]) && StringUtils.isNotBlank(addtime[1])) {
				SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
				Date date = sdf.parse(addtime[0].replace('T', ' '));
				Date date2 = sdf.parse(addtime[1].replace('T', ' '));

				Calendar calendar = new GregorianCalendar();
				calendar.setTime(date);
				calendar.add(calendar.DATE, 1); // 把日期往后增加一天,整数 往后推,负数往前移动
				date = calendar.getTime();

				Calendar calendar2 = new GregorianCalendar();
				calendar2.setTime(date2);
				calendar2.add(calendar2.DATE, 2); // 把日期往后增加两天,整数 往后推,负数往前移动
				date2 = calendar2.getTime();
				wrapper.between("a.addtime", date, date2);
			}
		}

		wrapper.orderByDesc("receiptkey");

		IPage<ReceiptEntity> page = baseDao.selectPageBySql(getPage(param, null, false), wrapper);
		return getPageData(page, ReceiptEntity.class);
	}

	@Override
	public PageData<ReceiptEntity> pageBySql(Map<String, Object> params) {
		IPage<ReceiptEntity> page = baseDao.selectPageBySql(getPage(params, null, false), getWrapper(params));
		return getPageData(page, ReceiptEntity.class);
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public String closeReceipt(String receiptkey) {
		// 将收货明细和收货表的状态改未11
		ReceiptEntity receiptEntity = new ReceiptEntity();
		receiptEntity.setStatus(ReceiptConstant.RECEIPTSTATUS_11);
		UpdateWrapper<ReceiptEntity> receiptUpdateWrapper = new UpdateWrapper<ReceiptEntity>();
		receiptUpdateWrapper.in("receiptkey", Arrays.asList(receiptkey));

		ReceiptdetailEntity receiptdetailEntity = new ReceiptdetailEntity();
		receiptdetailEntity.setStatus(ReceiptConstant.RECEIPTSTATUS_11);

		UpdateWrapper<ReceiptdetailEntity> ReceiptdetailUpdateWrapper = new UpdateWrapper<ReceiptdetailEntity>();
		ReceiptdetailUpdateWrapper.in("receiptkey", Arrays.asList(receiptkey));

		ReceiptdetaillistEntity receiptdetaillistEntity = new ReceiptdetaillistEntity();
		receiptdetaillistEntity.setStatus(ReceiptConstant.RECEIPTSTATUS_11);

		UpdateWrapper<ReceiptdetaillistEntity> ReceiptdetaillistEntityUpdateWrapper = new UpdateWrapper<ReceiptdetaillistEntity>();
		ReceiptdetaillistEntityUpdateWrapper.in("receiptkey", Arrays.asList(receiptkey));

		receiptdetaillistDao.update(receiptdetaillistEntity, ReceiptdetaillistEntityUpdateWrapper);
		receiptdetailDao.update(receiptdetailEntity, ReceiptdetailUpdateWrapper);
		baseDao.update(receiptEntity, receiptUpdateWrapper);

		return null;
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public String cancelReaceipt(String receiptkey) {
		// 关闭的单据不能撤销
		QueryWrapper<ReceiptEntity> receiptQuery = new QueryWrapper<ReceiptEntity>();
		receiptQuery.eq("receiptkey", receiptkey);

		ReceiptEntity receiptEntity = baseDao.selectOne(receiptQuery);
		if (receiptEntity.getStatus().equals(ReceiptConstant.RECEIPTSTATUS_11)
		// || receiptEntity.getStatus().equals(ReceiptConstant.RECEIPTSTATUS_15)
		) {
			// 回滚事务
			TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
			return MEGConstant.RECEIPT_NOT_CANCEL;
		}

		// 查询收货的库存记录是否发生改变，如果发生了改变，不能撤销收货
		// 查询收货记录数

		// Integer receiptdetaillistCount =
		// receiptdetaillistDao.selectCount(receiptdetaillistQuery);

		// 查收货与库存记录匹配的记录
		Integer count = receiptdetaillistDao.getReceiptAndInventory(receiptkey, null, null);
		if (count > 0) {
			// 回滚事务
			TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
			return MEGConstant.INVENTORY_HAS_CHANGE;
		}

		// 是否生成了上架任务
		Integer count1 = receiptdetaillistDao.hasCreatePutaway(receiptkey);
		if (count1 > 0) {
			// 回滚事务
			TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
			return MEGConstant.HAS_CREATE_PUTAWAY;
		}

		// 查询收货明细记录，然后对收货明细记录进行删除库存记录操作
		QueryWrapper<ReceiptdetaillistEntity> receiptdetaillistQuery = new QueryWrapper<ReceiptdetaillistEntity>();
		receiptdetaillistQuery.eq("receiptkey", receiptkey);

		List<ReceiptdetaillistEntity> receiptdetaillist = receiptdetaillistDao.selectList(receiptdetaillistQuery);
		for (ReceiptdetaillistEntity receiptdetaillistEntity : receiptdetaillist) {
			// 查询与收货记录对应的库存记录
			QueryWrapper<InventoryEntity> InventoryQuery = new QueryWrapper<InventoryEntity>();
			InventoryQuery.eq("sku", receiptdetaillistEntity.getSku());
			InventoryQuery.eq("location", receiptdetaillistEntity.getToloc());
			InventoryQuery.eq("tray", receiptdetaillistEntity.getTotray());
			InventoryQuery.eq("box", receiptdetaillistEntity.getTobox());
			InventoryQuery.eq("lot", receiptdetaillistEntity.getLot());

			List<InventoryEntity> inventorylist = inventoryDao.selectList(InventoryQuery);
			for (InventoryEntity inventoryEntity : inventorylist) {
				BigDecimal actqtyReceipt = receiptdetaillistEntity.getActqty();
				BigDecimal qtyInventory = inventoryEntity.getQty();

				if (actqtyReceipt.compareTo(qtyInventory) > 0) {
					// 回滚事务
					TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
					return MEGConstant.INVENTORY_HAS_CHANGE;
				}

				// 执行库存扣减
				InventoryEntity substractInventoryEntity = new InventoryEntity();
				substractInventoryEntity.setQty(qtyInventory.subtract(actqtyReceipt));

				UpdateWrapper<InventoryEntity> updateInventoryWrapper = new UpdateWrapper<InventoryEntity>();
				updateInventoryWrapper.eq("sku", inventoryEntity.getSku());
				updateInventoryWrapper.eq("location", inventoryEntity.getLocation());
				updateInventoryWrapper.eq("tray", inventoryEntity.getTray());
				updateInventoryWrapper.eq("box", inventoryEntity.getBox());
				updateInventoryWrapper.eq("lot", inventoryEntity.getLot());

				inventoryDao.update(substractInventoryEntity, updateInventoryWrapper);
				inventoryEntity.setQty(qtyInventory.subtract(actqtyReceipt));

				// 执行扣减收货明细记录的数据
				ReceiptdetaillistEntity substractReceiptdetaillistEntity = new ReceiptdetaillistEntity();
				substractReceiptdetaillistEntity.setActqty(actqtyReceipt.subtract(actqtyReceipt));

				UpdateWrapper<ReceiptdetaillistEntity> updateReceiptdetaillistWrapper = new UpdateWrapper<ReceiptdetaillistEntity>();
				updateReceiptdetaillistWrapper.eq("receiptkey", receiptdetaillistEntity.getReceiptkey());
				updateReceiptdetaillistWrapper.eq("line", receiptdetaillistEntity.getLine());
				updateReceiptdetaillistWrapper.eq("linelist", receiptdetaillistEntity.getLinelist());
				receiptdetaillistDao.update(substractReceiptdetaillistEntity, updateReceiptdetaillistWrapper);
				// 设置当前的明细记录的actqty
				receiptdetaillistEntity.setActqty(actqtyReceipt.subtract(actqtyReceipt));

				// 扣减明细的数据
//				ReceiptdetailEntity substractReceiptdetailEntity = new ReceiptdetailEntity();
//				substractReceiptdetailEntity.setActqty(actqtyReceipt.subtract(actqtyReceipt));

				UpdateWrapper<ReceiptdetailEntity> updateReceiptdetailWrapper = new UpdateWrapper<ReceiptdetailEntity>();
				updateReceiptdetailWrapper.eq("receiptkey", receiptdetaillistEntity.getReceiptkey());
				updateReceiptdetailWrapper.eq("line", receiptdetaillistEntity.getLine());
				updateReceiptdetailWrapper.setSql(" actqty = actqty-" + actqtyReceipt.longValue());
				receiptdetailDao.update(null, updateReceiptdetailWrapper);

				// 增加交易记录
				TradeEntity addTradeEntity = new TradeEntity();
				String tradekey = sequenceService.getSequence("trade");
				addTradeEntity.setTradekey(tradekey);
				addTradeEntity.setTradetype(TradeConstant.TRADETYPE_CANCELPA);
				addTradeEntity.setSku(receiptdetaillistEntity.getSku());
				addTradeEntity.setQty(actqtyReceipt.negate()); // 改为负数
				addTradeEntity.setSourcetype("");
				addTradeEntity.setSourcekey(receiptdetaillistEntity.getReceiptkey());
				addTradeEntity.setSourceline(receiptdetaillistEntity.getLine());
				addTradeEntity.setSourcelinelist(receiptdetaillistEntity.getLinelist());
				addTradeEntity.setFromlocation("");
				addTradeEntity.setTolocation(receiptdetaillistEntity.getToloc());
				addTradeEntity.setFromtray("");
				addTradeEntity.setTotray(receiptdetaillistEntity.getTotray());
				addTradeEntity.setFrombox("");
				addTradeEntity.setTobox(receiptdetaillistEntity.getTobox());
				addTradeEntity.setFromlot("");
				addTradeEntity.setTolot(receiptdetaillistEntity.getLot());
				addTradeEntity.setFromlot1(null);
				addTradeEntity.setFromlot2(null);
				addTradeEntity.setFromlot3("");
				addTradeEntity.setFromlot4("");
				addTradeEntity.setFromlot5("");
				addTradeEntity.setFromlot6("");
				addTradeEntity.setFromlot7("");
				addTradeEntity.setFromlot8("");
				addTradeEntity.setFromlot9("");
				addTradeEntity.setFromlot10("");
				addTradeEntity.setFromlot11("");
				addTradeEntity.setFromlot12("");
				addTradeEntity.setFromlot13("");
				addTradeEntity.setFromlot14("");
				addTradeEntity.setFromlot15("");
				addTradeEntity.setTolot1(receiptdetaillistEntity.getLot1());
				addTradeEntity.setTolot2(receiptdetaillistEntity.getLot2());
				addTradeEntity.setTolot3(receiptdetaillistEntity.getLot3());
				addTradeEntity.setTolot4(receiptdetaillistEntity.getLot4());
				addTradeEntity.setTolot5(receiptdetaillistEntity.getLot5());
				addTradeEntity.setTolot6(receiptdetaillistEntity.getLot6());
				addTradeEntity.setTolot7(receiptdetaillistEntity.getLot7());
				addTradeEntity.setTolot8(receiptdetaillistEntity.getLot8());
				addTradeEntity.setTolot9(receiptdetaillistEntity.getLot9());
				addTradeEntity.setTolot10(receiptdetaillistEntity.getLot10());
				addTradeEntity.setTolot11(receiptdetaillistEntity.getLot11());
				addTradeEntity.setTolot12(receiptdetaillistEntity.getLot12());
				addTradeEntity.setTolot13(receiptdetaillistEntity.getLot13());
				addTradeEntity.setTolot14(receiptdetaillistEntity.getLot14());
				addTradeEntity.setTolot15(receiptdetaillistEntity.getLot15());
				addTradeEntity.setRemark(receiptdetaillistEntity.getRemark());
				tradeDao.insert(addTradeEntity);
			}

			// 删掉唯一码
			deleteUniqueCode(receiptdetaillistEntity);

		}

//		变更单据状态
		// 变更明细记录状态为15
		ReceiptdetaillistEntity updateStatusReceiptdetaillistEntity = new ReceiptdetaillistEntity();
		updateStatusReceiptdetaillistEntity.setStatus(ReceiptConstant.RECEIPTSTATUS_15);

		UpdateWrapper<ReceiptdetaillistEntity> updateStatusReceiptdetaillistWrapper = new UpdateWrapper<ReceiptdetaillistEntity>();
		updateStatusReceiptdetaillistWrapper.eq("receiptkey", receiptkey);

		// 变更明细状态为15
		ReceiptdetailEntity updateStatusReceiptdetailEntity = new ReceiptdetailEntity();
		updateStatusReceiptdetailEntity.setStatus(ReceiptConstant.RECEIPTSTATUS_15);

		UpdateWrapper<ReceiptdetailEntity> updateStatusReceiptdetailEntityWrapper = new UpdateWrapper<ReceiptdetailEntity>();
		updateStatusReceiptdetailEntityWrapper.eq("receiptkey", receiptkey);

		// 变更主表状态为15
		ReceiptEntity updateStatusReceiptEntity = new ReceiptEntity();
		updateStatusReceiptEntity.setStatus(ReceiptConstant.RECEIPTSTATUS_15);

		UpdateWrapper<ReceiptEntity> updateStatusReceiptEntityWrapper = new UpdateWrapper<ReceiptEntity>();
		updateStatusReceiptEntityWrapper.eq("receiptkey", receiptkey);

		receiptdetaillistDao.update(updateStatusReceiptdetaillistEntity, updateStatusReceiptdetaillistWrapper);

		receiptdetailDao.update(updateStatusReceiptdetailEntity, updateStatusReceiptdetailEntityWrapper);

		baseDao.update(updateStatusReceiptEntity, updateStatusReceiptEntityWrapper);

		return null;
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public String redoReaceipt(String receiptkey) {
		// 没关闭的单据不能重新激活
		QueryWrapper<ReceiptEntity> receiptQuery = new QueryWrapper<ReceiptEntity>();
		receiptQuery.eq("receiptkey", receiptkey);

		ReceiptEntity receiptEntity = baseDao.selectOne(receiptQuery);
		if (!receiptEntity.getStatus().equals(ReceiptConstant.RECEIPTSTATUS_11)
				&& !receiptEntity.getStatus().equals(ReceiptConstant.RECEIPTSTATUS_15)) {

			return MEGConstant.RECEIPT_NOT_REDO;
		}

		// 更新明细记录的状态为9
		UpdateWrapper<ReceiptdetaillistEntity> receiptdetaillistUpdate = new UpdateWrapper<ReceiptdetaillistEntity>();
		receiptdetaillistUpdate.eq("receiptkey", receiptkey);
		ReceiptdetaillistEntity UpdateStatusReceiptdetaillistEntity = new ReceiptdetaillistEntity();
		UpdateStatusReceiptdetaillistEntity.setStatus(ReceiptConstant.RECEIPTSTATUS_9);
		receiptdetaillistDao.update(UpdateStatusReceiptdetaillistEntity, receiptdetaillistUpdate);

		// 查询明细，更改状态
		QueryWrapper<ReceiptdetailEntity> receiptdetailQuery = new QueryWrapper<ReceiptdetailEntity>();
		receiptdetailQuery.eq("receiptkey", receiptkey);
		List<ReceiptdetailEntity> receiptdetails = receiptdetailDao.selectList(receiptdetailQuery);

		boolean exists_0 = false;
		boolean exists_5 = false;
		boolean exists_9 = false;

		for (ReceiptdetailEntity receiptdetailEntity : receiptdetails) {
			ReceiptdetailEntity UpdateStatusReceiptdetailEntity = new ReceiptdetailEntity();
			BigDecimal qty = receiptdetailEntity.getQty();
			BigDecimal actqty = receiptdetailEntity.getActqty();

			if (qty.compareTo(actqty) < 0) {
				UpdateStatusReceiptdetailEntity.setStatus(ReceiptConstant.RECEIPTSTATUS_9);
				exists_9 = true;
			} else if (qty.compareTo(actqty) > 0) {
				if (actqty.compareTo(new BigDecimal(0)) == 0) {
					UpdateStatusReceiptdetailEntity.setStatus(ReceiptConstant.RECEIPTSTATUS_0);
					exists_0 = true;
				} else {
					UpdateStatusReceiptdetailEntity.setStatus(ReceiptConstant.RECEIPTSTATUS_5);
					exists_5 = true;
				}
			} else if (qty.compareTo(actqty) == 0) {
				if (actqty.compareTo(new BigDecimal(0)) == 0) {
					UpdateStatusReceiptdetailEntity.setStatus(ReceiptConstant.RECEIPTSTATUS_0);
					exists_0 = true;
				} else {
					UpdateStatusReceiptdetailEntity.setStatus(ReceiptConstant.RECEIPTSTATUS_9);
					exists_9 = true;
				}
			}

			UpdateWrapper<ReceiptdetailEntity> receiptdetailUpateWrapper = new UpdateWrapper<ReceiptdetailEntity>();
			receiptdetailUpateWrapper.eq("receiptkey", receiptdetailEntity.getReceiptkey());
			receiptdetailUpateWrapper.eq("line", receiptdetailEntity.getLine());
			receiptdetailDao.update(UpdateStatusReceiptdetailEntity, receiptdetailUpateWrapper);
		}

		ReceiptEntity updateStatusReceiptEntity = new ReceiptEntity();
		if (exists_5 || (exists_0 && exists_9)) {
			updateStatusReceiptEntity.setStatus(ReceiptConstant.RECEIPTSTATUS_5);
		} else if (exists_0 && !exists_5 && !exists_9) {
			updateStatusReceiptEntity.setStatus(ReceiptConstant.RECEIPTSTATUS_0);
		} else if (exists_9 && !exists_5 && !exists_0) {
			updateStatusReceiptEntity.setStatus(ReceiptConstant.RECEIPTSTATUS_9);
		}
		UpdateWrapper<ReceiptEntity> receiptUpdateWrapper = new UpdateWrapper<>();
		receiptUpdateWrapper.eq("receiptkey", receiptkey);
		baseDao.update(updateStatusReceiptEntity, receiptUpdateWrapper);

		return null;
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public String cancelReaceipt2(ReceiptdetaillistEntity entity) {
		// 关闭的单据不能撤销
		String receiptkey = entity.getReceiptkey();
		Integer line = entity.getLine();
		Integer linelist = entity.getLinelist();

		QueryWrapper<ReceiptEntity> receiptQuery = new QueryWrapper<ReceiptEntity>();
		receiptQuery.eq(StringUtils.isNotBlank(receiptkey), "receiptkey", receiptkey);
		/*
		 * receiptQuery.eq( StringUtils.isNotBlank(receiptkey) , "line", line);
		 * receiptQuery.eq( StringUtils.isNotBlank(receiptkey) , "linelist", linelist);
		 */
		// receiptQuery.eq( id!=null , "id", id);

		ReceiptEntity receiptEntity = baseDao.selectOne(receiptQuery);
		if (receiptEntity.getStatus().equals(ReceiptConstant.RECEIPTSTATUS_11)
				|| receiptEntity.getStatus().equals(ReceiptConstant.RECEIPTSTATUS_15)) {
			// 回滚事务
			TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
			return MEGConstant.RECEIPT_NOT_CANCEL;
		}

		// 查询收货明细记录，然后对收货明细记录进行删除库存记录操作
		// 查询收货记录数
		QueryWrapper<ReceiptdetaillistEntity> receiptdetaillistQuery = new QueryWrapper<ReceiptdetaillistEntity>();
		receiptdetaillistQuery.eq("receiptkey", receiptkey);
		receiptdetaillistQuery.eq("line", line);
		receiptdetaillistQuery.eq("linelist", linelist);
		ReceiptdetaillistEntity receiptdetaillistEntity = receiptdetaillistDao.selectOne(receiptdetaillistQuery);

		BigDecimal actqty = receiptdetaillistEntity.getActqty();
		if (actqty.compareTo(BigDecimal.ZERO) <= 0) {
			return null;
		}

		// 查收货与库存记录匹配的记录

		Integer count = receiptdetaillistDao.getReceiptAndInventory(receiptkey, line, linelist);
		if (count > 0) { // 回滚事务
			TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
			return MEGConstant.INVENTORY_HAS_CHANGE;
		}

		// 是否生成了上架任务
		QueryWrapper<TaskEntity> queryTask = new QueryWrapper<TaskEntity>();
		queryTask.lt("status", TaskConstant.TASK_9);
		queryTask.and(wrapper -> wrapper.eq("fromtray", receiptdetaillistEntity.getTotray()).or().eq("totray",
				receiptdetaillistEntity.getTotray()));

//		Integer count1 = receiptdetaillistDao.hasCreatePutaway(receiptkey);
		Integer count1 = taskDao.selectCount(queryTask);
		if (count1 > 0) {
			// 回滚事务
			TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
			return MEGConstant.HAS_CREATE_PUTAWAY;
		}

		String uniquecode = receiptdetaillistEntity.getUniquecode();
		if (StringUtils.isNoneBlank(uniquecode)) {
			String[] uniquedes = uniquecode.split("\n");
			for (String item : uniquedes) {
				QueryWrapper<UniquecodeEntity> entityQueryWrapper = new QueryWrapper<>();
				entityQueryWrapper.eq("uniquecode", item);
				uniquecodeDao.delete(entityQueryWrapper);
			}
		}
		QueryWrapper<InventoryEntity> InventoryQuery = new QueryWrapper<InventoryEntity>();
		InventoryQuery.eq("sku", receiptdetaillistEntity.getSku());
		InventoryQuery.eq("location", receiptdetaillistEntity.getToloc());
		InventoryQuery.eq("tray", receiptdetaillistEntity.getTotray());
		InventoryQuery.eq("box", receiptdetaillistEntity.getTobox());
		InventoryQuery.eq("lot", receiptdetaillistEntity.getLot());

		InventoryEntity inventoryEntity = inventoryDao.selectOne(InventoryQuery);

		BigDecimal actqtyReceipt = receiptdetaillistEntity.getActqty();
		BigDecimal qtyInventory = inventoryEntity.getQty();

		if (actqtyReceipt.compareTo(qtyInventory) > 0) {
			// 回滚事务
			TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
			return MEGConstant.INVENTORY_HAS_CHANGE;
		}

		// 执行库存扣减
		InventoryEntity substractInventoryEntity = new InventoryEntity();
		substractInventoryEntity.setQty(qtyInventory.subtract(actqtyReceipt));

		UpdateWrapper<InventoryEntity> updateInventoryWrapper = new UpdateWrapper<InventoryEntity>();
		updateInventoryWrapper.eq("sku", inventoryEntity.getSku());
		updateInventoryWrapper.eq("location", inventoryEntity.getLocation());
		updateInventoryWrapper.eq("tray", inventoryEntity.getTray());
		updateInventoryWrapper.eq("box", inventoryEntity.getBox());
		updateInventoryWrapper.eq("lot", inventoryEntity.getLot());

		inventoryDao.update(substractInventoryEntity, updateInventoryWrapper);
		inventoryEntity.setQty(qtyInventory.subtract(actqtyReceipt));

		// 执行扣减收货明细记录的数据
		ReceiptdetaillistEntity substractReceiptdetaillistEntity = new ReceiptdetaillistEntity();
		substractReceiptdetaillistEntity.setActqty(actqtyReceipt.subtract(actqtyReceipt));

		UpdateWrapper<ReceiptdetaillistEntity> updateReceiptdetaillistWrapper = new UpdateWrapper<ReceiptdetaillistEntity>();
		updateReceiptdetaillistWrapper.eq("receiptkey", receiptdetaillistEntity.getReceiptkey());
		updateReceiptdetaillistWrapper.eq("line", receiptdetaillistEntity.getLine());
		updateReceiptdetaillistWrapper.eq("linelist", receiptdetaillistEntity.getLinelist());
		receiptdetaillistDao.update(substractReceiptdetaillistEntity, updateReceiptdetaillistWrapper);
		// 设置当前的明细记录的actqty
		receiptdetaillistEntity.setActqty(actqtyReceipt.subtract(actqtyReceipt));

		// 扣减明细的数据
//				ReceiptdetailEntity substractReceiptdetailEntity = new ReceiptdetailEntity();
//				substractReceiptdetailEntity.setActqty(actqtyReceipt.subtract(actqtyReceipt));

		UpdateWrapper<ReceiptdetailEntity> updateReceiptdetailWrapper = new UpdateWrapper<ReceiptdetailEntity>();
		updateReceiptdetailWrapper.eq("receiptkey", receiptdetaillistEntity.getReceiptkey());
		updateReceiptdetailWrapper.eq("line", receiptdetaillistEntity.getLine());
		updateReceiptdetailWrapper.setSql(" actqty = actqty-" + actqtyReceipt.longValue());
		receiptdetailDao.update(null, updateReceiptdetailWrapper);

		// 增加交易记录
		TradeEntity addTradeEntity = new TradeEntity();
		String tradekey = sequenceService.getSequence("trade");
		addTradeEntity.setTradekey(tradekey);
		addTradeEntity.setTradetype(TradeConstant.TRADETYPE_CANCELPA);
		addTradeEntity.setSku(receiptdetaillistEntity.getSku());
		addTradeEntity.setQty(actqtyReceipt.negate()); // 改为负数
		addTradeEntity.setSourcetype("");
		addTradeEntity.setSourcekey(receiptdetaillistEntity.getReceiptkey());
		addTradeEntity.setSourceline(receiptdetaillistEntity.getLine());
		addTradeEntity.setSourcelinelist(receiptdetaillistEntity.getLinelist());
		addTradeEntity.setFromlocation("");
		addTradeEntity.setTolocation(receiptdetaillistEntity.getToloc());
		addTradeEntity.setFromtray("");
		addTradeEntity.setTotray(receiptdetaillistEntity.getTotray());
		addTradeEntity.setFrombox("");
		addTradeEntity.setTobox(receiptdetaillistEntity.getTobox());
		addTradeEntity.setFromlot("");
		addTradeEntity.setTolot(receiptdetaillistEntity.getLot());
		addTradeEntity.setFromlot1(null);
		addTradeEntity.setFromlot2(null);
		addTradeEntity.setFromlot3("");
		addTradeEntity.setFromlot4("");
		addTradeEntity.setFromlot5("");
		addTradeEntity.setFromlot6("");
		addTradeEntity.setFromlot7("");
		addTradeEntity.setFromlot8("");
		addTradeEntity.setFromlot9("");
		addTradeEntity.setFromlot10("");
		addTradeEntity.setFromlot11("");
		addTradeEntity.setFromlot12("");
		addTradeEntity.setFromlot13("");
		addTradeEntity.setFromlot14("");
		addTradeEntity.setFromlot15("");
		addTradeEntity.setTolot1(receiptdetaillistEntity.getLot1());
		addTradeEntity.setTolot2(receiptdetaillistEntity.getLot2());
		addTradeEntity.setTolot3(receiptdetaillistEntity.getLot3());
		addTradeEntity.setTolot4(receiptdetaillistEntity.getLot4());
		addTradeEntity.setTolot5(receiptdetaillistEntity.getLot5());
		addTradeEntity.setTolot6(receiptdetaillistEntity.getLot6());
		addTradeEntity.setTolot7(receiptdetaillistEntity.getLot7());
		addTradeEntity.setTolot8(receiptdetaillistEntity.getLot8());
		addTradeEntity.setTolot9(receiptdetaillistEntity.getLot9());
		addTradeEntity.setTolot10(receiptdetaillistEntity.getLot10());
		addTradeEntity.setTolot11(receiptdetaillistEntity.getLot11());
		addTradeEntity.setTolot12(receiptdetaillistEntity.getLot12());
		addTradeEntity.setTolot13(receiptdetaillistEntity.getLot13());
		addTradeEntity.setTolot14(receiptdetaillistEntity.getLot14());
		addTradeEntity.setTolot15(receiptdetaillistEntity.getLot15());
		addTradeEntity.setRemark(receiptdetaillistEntity.getRemark());
		tradeDao.insert(addTradeEntity);

		// 查询是否录入了唯一码，如果有数据录入，则删掉唯一码
		deleteUniqueCode(receiptdetaillistEntity);

		/* } */

//		变更单据状态
		// 变更明细记录状态为15
		ReceiptdetaillistEntity updateStatusReceiptdetaillistEntity = new ReceiptdetaillistEntity();
		updateStatusReceiptdetaillistEntity.setStatus(ReceiptConstant.RECEIPTSTATUS_0);

		UpdateWrapper<ReceiptdetaillistEntity> updateStatusReceiptdetaillistWrapper = new UpdateWrapper<ReceiptdetaillistEntity>();
		updateStatusReceiptdetaillistWrapper.eq("receiptkey", receiptkey);
		updateStatusReceiptdetaillistWrapper.eq("line", line);
		updateStatusReceiptdetaillistWrapper.eq("linelist", linelist);
		receiptdetaillistDao.update(updateStatusReceiptdetaillistEntity, updateStatusReceiptdetaillistWrapper);
		// receiptdetaillistDao.delete(updateStatusReceiptdetaillistWrapper) ;

		// 变更明细状态为15
//		ReceiptdetailEntity updateStatusReceiptdetailEntity = new ReceiptdetailEntity();
//		updateStatusReceiptdetailEntity.setStatus(ReceiptConstant.RECEIPTSTATUS_15);
//
//		UpdateWrapper<ReceiptdetailEntity> updateStatusReceiptdetailEntityWrapper = new UpdateWrapper<ReceiptdetailEntity>();
//		updateStatusReceiptdetailEntityWrapper.eq("receiptkey", receiptkey);
//
//		// 变更主表状态为15
//		ReceiptEntity updateStatusReceiptEntity = new ReceiptEntity();
//		updateStatusReceiptEntity.setStatus(ReceiptConstant.RECEIPTSTATUS_15);
//
//		UpdateWrapper<ReceiptEntity> updateStatusReceiptEntityWrapper = new UpdateWrapper<ReceiptEntity>();
//		updateStatusReceiptEntityWrapper.eq("receiptkey", receiptkey);

//
//		receiptdetailDao.update(updateStatusReceiptdetailEntity, updateStatusReceiptdetailEntityWrapper);
//
//		baseDao.update(updateStatusReceiptEntity, updateStatusReceiptEntityWrapper);

		return null;
	}

	private void deleteUniqueCode(ReceiptdetaillistEntity receiptdetaillistEntity) {
		QueryWrapper<UniquecoderecordEntity> queryUniquecoderecord = new QueryWrapper<UniquecoderecordEntity>();
		queryUniquecoderecord.eq("sourceid", receiptdetaillistEntity.getId());
		queryUniquecoderecord.eq("sourcekey", receiptdetaillistEntity.getReceiptkey());
		queryUniquecoderecord.eq("sourceline", receiptdetaillistEntity.getLine());
		queryUniquecoderecord.eq("sourcelinelist", receiptdetaillistEntity.getLinelist());
		List<UniquecoderecordEntity> listUniquecoderecord = uniquecoderecordDao.selectList(queryUniquecoderecord);
		for (UniquecoderecordEntity uniquecoderecordEntity : listUniquecoderecord) {
			String tray = uniquecoderecordEntity.getTotray();
			String box = uniquecoderecordEntity.getTobox();
			String sku = uniquecoderecordEntity.getSku();
			String lot = uniquecoderecordEntity.getLot();
			String uniqueCode = uniquecoderecordEntity.getUniquecode();

			UpdateWrapper<UniquecodeEntity> queryUniquecode = new UpdateWrapper<UniquecodeEntity>();
			queryUniquecode.eq("uniqueCode", uniqueCode);
			queryUniquecode.eq("sku", sku);
			queryUniquecode.eq("tray", tray);
			queryUniquecode.eq("box", box);
			queryUniquecode.eq("lot", lot);
			uniquecodeDao.delete(queryUniquecode);

			UniquecoderecordEntity uniquecoderecordEntityIn = new UniquecoderecordEntity();
			uniquecoderecordEntityIn.setUniquecode(uniqueCode);
			uniquecoderecordEntityIn.setFromtray(tray);
			uniquecoderecordEntityIn.setFrombox(box);
			uniquecoderecordEntityIn.setTotray(StringUtils.EMPTY);
			uniquecoderecordEntityIn.setTobox(StringUtils.EMPTY);
			uniquecoderecordEntityIn.setSku(sku);
			uniquecoderecordEntityIn.setLot(lot);
			uniquecoderecordEntityIn.setSourcetype(TradeConstant.TRADETYPE_CANCELPA);
			uniquecoderecordEntityIn.setSourcetable("receiptdetaillist");
			uniquecoderecordEntityIn.setSourceid(receiptdetaillistEntity.getId());
			uniquecoderecordEntityIn.setSourcekey(receiptdetaillistEntity.getReceiptkey());
			uniquecoderecordEntityIn.setSourceline(receiptdetaillistEntity.getLine());
			uniquecoderecordEntityIn.setSourcelinelist(receiptdetaillistEntity.getLinelist());
			uniquecoderecordDao.insert(uniquecoderecordEntityIn);
		}
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public String qcCreate(Map map) {
		if (map == null) {
			return "请输入二维码信息";
		}
		String qc = (String) map.get("QC");
		if (StringUtils.isBlank(qc)) {
			return "请输入二维码信息";
		}

		String[] qcs = qc.split(";");
		if (qcs.length != 8) {
			return "二维码格式不对,不能生成入库单";
		}
		String planNo = qcs[0];
		String taskNo = qcs[1];
		String skuNo = qcs[2];
		String preqtyStr = qcs[3];
		String luNo = qcs[4];
		String cailiaoNo = qcs[5];
		String paiNo = qcs[6];
		String skuName = qcs[7];

		// 根据二维码第三条数据的前缀确定物料类别
		String skusort = "";
		if (skuNo.startsWith("1")) {
			skusort = "1/";
		} else if (skuNo.startsWith("2")) {
			skusort = "2/";
		}

		if (StringUtils.isBlank(skuNo)) {
			return "二维码中的物料编码为空,不能生成入库单";
		}

		BigDecimal qty = null;
		try {
			qty = new BigDecimal(preqtyStr);
		} catch (NumberFormatException e) {
			return "二维码中的数量的格式不正确,不能生成入库单";
		}

		// 校验物料编码是否存在，若不存在，就新增物料
		QueryWrapper<SkuEntity> querySku = new QueryWrapper<SkuEntity>();
		querySku.eq("sku", skuNo);
		SkuEntity skuEntity = skuDao.selectOne(querySku);
		if (skuEntity == null) {
			SkuEntity skuEntityInsert = new SkuEntity();
			skuEntityInsert.setSku(skuNo);
			skuEntityInsert.setDescr(skuName);
			skuEntityInsert.setSort(skusort);
			skuEntityInsert.setStrategykey("0000000002");
			skuEntityInsert.setAllocationstrategy("N01");
			skuEntityInsert.setRolateby("lot1");
			;
			skuDao.insert(skuEntityInsert);
		}

		String receiptkey = (String) map.get("receiptkey");
		if (StringUtils.isBlank(receiptkey)) {
			// 增加主表
			receiptkey = sequenceService.getSequence("receipt");
			ReceiptEntity receiptEntity = new ReceiptEntity();
			receiptEntity.setReceiptkey(receiptkey);
			receiptEntity.setStatus(ReceiptConstant.RECEIPTSTATUS_0);
			receiptEntity.setSourcekey(planNo);
			receiptEntity.setType("PR");
			;
			baseDao.insert(receiptEntity);
		}
		// 增加明细
		ReceiptdetailEntity receiptdetailEntity = new ReceiptdetailEntity();
		receiptdetailEntity.setReceiptkey(receiptkey);
		Integer count = receiptdetailDao.getReceiptdetailMaxLine(receiptkey);
		if (count == null) {
			count = 1;
		}
		receiptdetailEntity.setLine(count + 1);
		receiptdetailEntity.setSku(skuNo);
		receiptdetailEntity.setStatus(ReceiptConstant.RECEIPTSTATUS_0);
		receiptdetailEntity.setQty(qty);

		Calendar cal = Calendar.getInstance();
		java.text.SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		String cdate = sdf.format(cal.getTime());
		Date date = null;
		try {
			date = sdf.parse(cdate);
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		receiptdetailEntity.setLot1(date);
		receiptdetailEntity.setLot3(StringUtils.EMPTY);
		receiptdetailEntity.setLot4(StringUtils.EMPTY);
		receiptdetailEntity.setLot5(StringUtils.EMPTY);
		receiptdetailEntity.setLot6(planNo);
		receiptdetailEntity.setLot7(StringUtils.EMPTY);
		receiptdetailEntity.setLot8(taskNo);
		receiptdetailEntity.setLot9(StringUtils.EMPTY);
		receiptdetailEntity.setLot10(StringUtils.EMPTY);
		receiptdetailEntity.setLot11(luNo);
		receiptdetailEntity.setLot12(cailiaoNo);
		receiptdetailEntity.setLot13(paiNo);
		receiptdetailEntity.setLot14(StringUtils.EMPTY);
		receiptdetailEntity.setLot15(StringUtils.EMPTY);
		receiptdetailDao.insert(receiptdetailEntity);

		return null;
	}

	@Override
	public List<Map<String, Object>> getPrintQc(Map<String, Object> params) {

		List list1 = (List) params.get("receiptkeys");

		String receiptkey = "";
		List<Map<String, Object>> list = receiptdetaillistDao.getPrintQc(list1);
		return list;
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public String receiptEmptyLPN(Map<String, Object> params) {
		String tray = (String) params.get("tray");

		// 托盘为空
		if (StringUtils.isBlank(tray)) {
			return "请输入托盘号";
		}
		// 无效的托盘
		if (StringUtils.startsWithAny(tray, new String[] { "S", "M", "L" })) {

		} else {
			return "输入托盘号无效，请重新输入";
		}
		// 校验托盘是否存在任务
		QueryWrapper<TaskEntity> queryTask = new QueryWrapper<TaskEntity>();
		queryTask.lt("status", TaskConstant.TASK_9);
		queryTask.and(wrapper -> wrapper.eq("fromtray", tray).or().eq("totray", tray));

		Integer count1 = taskDao.selectCount(queryTask);
		if (count1 > 0) {
			// 回滚事务
//			TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
			return MEGConstant.HAS_CREATE_PUTAWAY;
		}

		String sku = "KX";
		// 查询批次信息
		// 查询是否存在相同批属性的lot记录
		// 存在：使用已有的lot号
		// 不存在：产生新的lot记录
		QueryWrapper<LotEntity> lotWrapper = new QueryWrapper<>();
		lotWrapper.eq("sku", sku);
		lotWrapper.eq("lot3", StringUtils.EMPTY);
		lotWrapper.eq("lot4", StringUtils.EMPTY);
		lotWrapper.eq("lot5", StringUtils.EMPTY);
		lotWrapper.eq("lot6", StringUtils.EMPTY);
		lotWrapper.eq("lot7", StringUtils.EMPTY);
		lotWrapper.eq("lot8", StringUtils.EMPTY);
		lotWrapper.eq("lot9", StringUtils.EMPTY);
		lotWrapper.eq("lot10", StringUtils.EMPTY);
		lotWrapper.eq("lot11", StringUtils.EMPTY);
		lotWrapper.eq("lot12", StringUtils.EMPTY);
		lotWrapper.eq("lot13", StringUtils.EMPTY);
		lotWrapper.eq("lot14", StringUtils.EMPTY);
		lotWrapper.eq("lot15", StringUtils.EMPTY);
		lotWrapper.and(wrapper -> wrapper.isNull("lot1"));
		lotWrapper.and(wrapper -> wrapper.isNull("lot2"));
		lotWrapper.orderByDesc("lot");

		// 批次数据
		String lot = "";
		List<LotEntity> LotEntityList = lotDao.selectList(lotWrapper);
		if (LotEntityList.size() > 0) {
			lot = LotEntityList.get(0).getLot();
		} else {
			LotEntity addLotEntity = new LotEntity();
			String lotNew = sequenceService.getSequence("lot");
			lot = lotNew;
			addLotEntity.setLot(lotNew);
			addLotEntity.setSku(sku);
			addLotEntity.setLot1(null);
			addLotEntity.setLot2(null);
			addLotEntity.setLot3(StringUtils.EMPTY);
			addLotEntity.setLot4(StringUtils.EMPTY);
			addLotEntity.setLot5(StringUtils.EMPTY);
			addLotEntity.setLot6(StringUtils.EMPTY);
			addLotEntity.setLot7(StringUtils.EMPTY);
			addLotEntity.setLot8(StringUtils.EMPTY);
			addLotEntity.setLot9(StringUtils.EMPTY);
			addLotEntity.setLot10(StringUtils.EMPTY);
			addLotEntity.setLot11(StringUtils.EMPTY);
			addLotEntity.setLot12(StringUtils.EMPTY);
			addLotEntity.setLot13(StringUtils.EMPTY);
			addLotEntity.setLot14(StringUtils.EMPTY);
			addLotEntity.setLot15(StringUtils.EMPTY);
			lotDao.insert(addLotEntity);
		}

		// 增加库存
		InventoryEntity addInventoryEntity = new InventoryEntity();
		addInventoryEntity.setSku(sku);
		addInventoryEntity.setLot(lot);
		addInventoryEntity.setLocation("PA");
		addInventoryEntity.setTray(tray);
		addInventoryEntity.setBox("B01");
		addInventoryEntity.setQty(new BigDecimal(1));
		addInventoryEntity.setPickqty(new BigDecimal(0));
		addInventoryEntity.setPutqty(new BigDecimal(0));
		addInventoryEntity.setStatus(0);
		addInventoryEntity.setRemark(StringUtils.EMPTY);
		inventoryDao.insert(addInventoryEntity);

		// 增加交易记录
		TradeEntity addTradeEntity = new TradeEntity();
		String tradekey = sequenceService.getSequence("trade");
		addTradeEntity.setTradekey(tradekey);
		addTradeEntity.setTradetype(TradeConstant.TRADETYPE_PA);
		addTradeEntity.setSku(sku);
		addTradeEntity.setQty(new BigDecimal(1));
		addTradeEntity.setSourcetype("");
		addTradeEntity.setSourcekey("");
		addTradeEntity.setSourceline(null);
		addTradeEntity.setSourcelinelist(null);
		addTradeEntity.setFromlocation("");
		addTradeEntity.setTolocation("PA");
		addTradeEntity.setFromtray("");
		addTradeEntity.setTotray(tray);
		addTradeEntity.setFrombox("");
		addTradeEntity.setTobox("B01");
		addTradeEntity.setFromlot1(null);
		addTradeEntity.setFromlot2(null);
		addTradeEntity.setFromlot3("");
		addTradeEntity.setFromlot4("");
		addTradeEntity.setFromlot5("");
		addTradeEntity.setFromlot6("");
		addTradeEntity.setFromlot7("");
		addTradeEntity.setFromlot8("");
		addTradeEntity.setFromlot9("");
		addTradeEntity.setFromlot10("");
		addTradeEntity.setFromlot11("");
		addTradeEntity.setFromlot12("");
		addTradeEntity.setFromlot13("");
		addTradeEntity.setFromlot14("");
		addTradeEntity.setFromlot15("");
		addTradeEntity.setTolot1(null);
		addTradeEntity.setTolot2(null);
		addTradeEntity.setTolot3(StringUtils.EMPTY);
		addTradeEntity.setTolot4(StringUtils.EMPTY);
		addTradeEntity.setTolot5(StringUtils.EMPTY);
		addTradeEntity.setTolot6(StringUtils.EMPTY);
		addTradeEntity.setTolot7(StringUtils.EMPTY);
		addTradeEntity.setTolot8(StringUtils.EMPTY);
		addTradeEntity.setTolot9(StringUtils.EMPTY);
		addTradeEntity.setTolot10(StringUtils.EMPTY);
		addTradeEntity.setTolot11(StringUtils.EMPTY);
		addTradeEntity.setTolot12(StringUtils.EMPTY);
		addTradeEntity.setTolot13(StringUtils.EMPTY);
		addTradeEntity.setTolot14(StringUtils.EMPTY);
		addTradeEntity.setTolot15(StringUtils.EMPTY);
		addTradeEntity.setRemark(StringUtils.EMPTY);
		addTradeEntity.setFromlot(null);
		addTradeEntity.setTolot(lot);
		tradeDao.insert(addTradeEntity);
		
		//生产上架任务
		receiptdetaillistService.getPutawayTask(addInventoryEntity,"");
		
		return null;
	}

}
